package cn.itsource.blog.controller;

import cn.itsource.blog.domain.ArticleType;
import cn.itsource.blog.query.ArticleTypeQuery;
import cn.itsource.blog.service.IArticleTypeService;
import cn.itsource.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {

    @Autowired
    private IArticleTypeService articleTypeService;

    @PatchMapping
    public JsonResult queryPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        try{
            return JsonResult.success(articleTypeService.pageInfo(articleTypeQuery));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PutMapping
    public JsonResult save(@RequestBody ArticleType articleType) {
        try{
            articleTypeService.save(articleType);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }



    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try{
            articleTypeService.delete(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PatchMapping("/batchDelete")
    public JsonResult batchDelete(@RequestBody List<ArticleType> users){
        try{
            articleTypeService.batchDelete(users);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 扎实文章类型结构
     * @return
     */
    @PatchMapping("/treeTypes")
    public JsonResult treeTypes(){
        try{
            return JsonResult.success(articleTypeService.treeTypes());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }




}
